package work.managercenter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Collections;

public class ComplexChars {


	 private static final String dbURL = "jdbc:mysql://172.16.231.19:3306/websimpl_aptrate";
		private static final String dbUser = "aptratings";
		private static final String dbPassword = "password";


		private static final Logger logger = Logger.getLogger(ComplexChars.class.getName());
		private static boolean driver_loaded = false;

	private synchronized static Connection getDBConnection() throws SQLException {

		if (!driver_loaded) {
			String driver = "com.mysql.jdbc.Driver";
			try {
				Class.forName(driver).newInstance();
				driver_loaded = true;
			} catch (Exception e) {
				logger.log(Level.SEVERE, null, e);
			}
		}

		return DriverManager.getConnection(dbURL, dbUser, dbPassword);
	}


	public static void main(String[] args) {
		try {
			printComplexChars();
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}


	private static void printComplexChars() throws SQLException {
		List<String> complexData = getComplexData();

		Set<Character> chars = new HashSet<Character>();
		for (String data: complexData) {
			for (int i = 0; i < data.length(); i++) {
				chars.add(data.charAt(i));
			}
		}


		List<Character> sortedChars = new ArrayList<Character>(chars);
		Collections.sort(sortedChars);

		StringBuilder sb = new StringBuilder();
		for (Character character : sortedChars) {
			sb.append(character).append(System.getProperty("line.separator"));
		}

		System.out.println(sb.toString());
	}


	private static List<String> getComplexData() throws SQLException {
		Connection connection = getDBConnection();
		PreparedStatement ps = connection.prepareStatement("select complex_name, city, state from aptcomplexes");
		ResultSet rs = ps.executeQuery();

		List<String> stuff = new ArrayList<String>();

		while (rs.next()) {
			String complexName = rs.getString("complex_name");
			String city = rs.getString("city");
			String state = rs.getString("state");


			stuff.add(complexName.toLowerCase());
			stuff.add(city.toLowerCase());
			stuff.add(state.toLowerCase());
		}

		rs.close();
		ps.close();
		connection.close();

		return stuff;
	}
}
